/**
 * 
 */
package com.yunyike.app.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.rop.annotation.IgnoreSignType;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.request.UploadFile;
import com.rop.response.Response;
import com.yunyike.app.constants.CommonConstant;
import com.yunyike.app.constants.OrderConstants;
import com.yunyike.app.data.dto.UserOrdersDTO;
import com.yunyike.app.data.model.OrderReceiptMapping;
import com.yunyike.app.data.model.User;
import com.yunyike.app.data.service.IOrderReceiptMappingService;
import com.yunyike.app.data.service.IOrderService;
import com.yunyike.app.redis.JedisManager;
import com.yunyike.app.request.UploadFileRequest;
import com.yunyike.app.util.UploadUtil;


@ServiceMethodBean(ignoreSign = IgnoreSignType.NO)
public class UploadFileService {

	private Logger log = LoggerFactory.getLogger(UploadFileService.class);

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IOrderReceiptMappingService iOrderReceiptMappingService;
	
	
	@ServiceMethod(method = "upload.image", version = "1.0", needInSession=NeedInSessionType.NO)
	public Response uploadPhoto(UploadFileRequest request) {
		log.info("upload。image==================");
		Response response = new Response();
		String shopperId = request.getShopperId();
		String orderId = request.getOrderId();
		String receiptNumber = request.getReceiptNumber();
		log.info("=======params：：receiptNumber=" + receiptNumber + ",shopperId=" + shopperId + ",orderId=" + orderId);

		User user = JedisManager.getObject(shopperId + CommonConstant.SHOPPER_CONNECT_USER, User.class);
		if (null == user) {
			log.info("user::::未获取到该导购的绑定用户shopperId=" + shopperId);
			response.getResult().setResult("0");
			response.getResult().setMessage("未获取到关联顾客!");
			return response;
		}
		log.info("user::::::" + user.getUserName() + "||" + user.getUserId());

		UserOrdersDTO dto = new UserOrdersDTO();
		dto.setUserId(user.getUserId());
		dto.setOrderId(orderId);
		dto.setOrderStatus(OrderConstants.ORDER_STATUS_0);
		List<UserOrdersDTO> orders = orderService.findUserOrders(dto);
		if (null == orders || orders.size() == 0) {
			log.info("order::::未获取到待支付订单orderId=" + orderId);
			response.getResult().setResult("0");
			response.getResult().setMessage("该订单不存在或已确认支付");
			return response;
		}
		
		try {

			if (null == request.getFile()) {
				response.getResult().setResult("0");
				response.getResult().setMessage("上传图片为空");
				return response;
			}
			UploadFile file = request.getFile();
			String webpath = UploadUtil.uploadFile(file, orders.get(0).getOrderNumber(), new Date());
			if (!"".equals(webpath)) {
				OrderReceiptMapping receiptImg = new OrderReceiptMapping();
				receiptImg.setReceiptImg(webpath);
				receiptImg.setOrderId(orderId);
				receiptImg.setReceiptNumber(receiptNumber);
				receiptImg.setReceiptImgDate(new Date());
				List<OrderReceiptMapping> orderReceipts = iOrderReceiptMappingService.selectByOrderId(orderId);
				if(orderReceipts==null||orderReceipts.size()==0){
					String orderReceiptMappingId = UUID.randomUUID().toString();
					receiptImg.setOrderReceiptMappingId(orderReceiptMappingId);
					iOrderReceiptMappingService.insert(receiptImg);
				}else{
					receiptImg.setOrderReceiptMappingId(orderReceipts.get(0).getOrderReceiptMappingId());
					iOrderReceiptMappingService.updateByPrimaryKeySelective(receiptImg);
				}
				

				response.getResult().setResult("1");
				response.getResult().setMessage("图片上传成功");

			} else {
				response.getResult().setResult("0");
				response.getResult().setMessage("图片上传失败");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			response.getResult().setMessage("error" );
			response.getResult().setResult("0");
		}
		return response;
	}
	
	
	
}
